package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MusicActivity extends AppCompatActivity {


    MyService1 myService1;

    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            MyService1.LocalBinder binder = (MyService1.LocalBinder) iBinder;
            myService1 = binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            myService1 = null;
        }
    };

//    Intent intent1 = new Intent(this, MyService1.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);

//        Intent intent = getIntent();

        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);

        Button button3 = (Button) findViewById(R.id.button3);

        Button button4 = (Button) findViewById(R.id.button4);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MusicActivity.this, MyService1.class);
                //  点击button1绑定MyService服务
                bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
                Log.d("xr", "绑定服务");
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MusicActivity.this, MyService1.class);
                Log.d("xr", "开启服务");
                startService(intent);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MusicActivity.this, MyService1.class);
                Log.d("xr", "停止服务");
                stopService(intent);
            }
        });

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("xr", "解除服务");
                unbindService(serviceConnection);
            }
        });
    }

}